Skip to content

Conversation

@jonathan-buttner
Copy link
Contributor

This PR refactors the OpenAI response parsing logic.

  • Switches to use a ConstructingObjectParser for the response parsing logic from OpenAI
  • Consolidates the OpenAI classes into external.openai

@jonathan-buttner jonathan-buttner added >non-issue :ml Machine learning Team:ML Meta label for the ML team auto-backport Automatically create backport pull requests when merged v8.19.0 v9.1.0 Feature:GenAI Features around GenAI labels Mar 7, 2025
ensureExpectedToken(XContentParser.Token.START_OBJECT, token, jsonParser);

positionParserAtTokenAfterField(jsonParser, "choices", FAILED_TO_FIND_FIELD_TEMPLATE);
try (var p = XContentFactory.xContent(XContentType.JSON).createParser(XContentParserConfiguration.EMPTY, response.body())) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The major changes here are to use the ConstructingObjectParser instead of iterating by token.

@@ -0,0 +1,107 @@
/*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class was moved and transitioned to use the ConstructingObjectParser.

@@ -1,110 +0,0 @@
/*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved to external.openai and transitioned to use the ConstructingObjectParser.

action.execute(new DocumentsOnlyInput(List.of("abc")), InferenceAction.Request.DEFAULT_TIMEOUT, listener);

var failureCauseMessage = "Failed to find required field [data] in OpenAI embeddings response";
var failureCauseMessage = "Required [data]";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we encounter a parsing failure where a field is missing, the error message will be decorated on the way back in one of the upstream listeners. The error message will have the openai information included elsewhere.

var failureCauseMessage = "Required [choices]";
var thrownException = expectThrows(ElasticsearchStatusException.class, () -> listener.actionGet(TIMEOUT));
assertThat(
thrownException.getMessage(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an example of the message containing openai and chat completions information already.

@jonathan-buttner jonathan-buttner marked this pull request as ready for review March 7, 2025 22:11
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/ml-core (Team:ML)

@jonathan-buttner jonathan-buttner merged commit 543c50a into elastic:main Mar 11, 2025
17 checks passed
@jonathan-buttner jonathan-buttner deleted the ml-refactor-openai-2 branch March 11, 2025 12:26
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.x

jonathan-buttner added a commit to jonathan-buttner/elasticsearch that referenced this pull request Mar 11, 2025
…class locations (elastic#124380)

* Switching openai to ConstructingObjectParser

* Moving files

* Fixing package errors
elasticsearchmachine pushed a commit that referenced this pull request Mar 11, 2025
…class locations (#124380) (#124561)

* Switching openai to ConstructingObjectParser

* Moving files

* Fixing package errors
albertzaharovits pushed a commit to albertzaharovits/elasticsearch that referenced this pull request Mar 13, 2025
…class locations (elastic#124380)

* Switching openai to ConstructingObjectParser

* Moving files

* Fixing package errors
jfreden pushed a commit to jfreden/elasticsearch that referenced this pull request Mar 13, 2025
…class locations (elastic#124380)

* Switching openai to ConstructingObjectParser

* Moving files

* Fixing package errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-backport Automatically create backport pull requests when merged Feature:GenAI Features around GenAI :ml Machine learning >non-issue Team:ML Meta label for the ML team v8.19.0 v9.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants